Create a Data File From a Generated Document
Fluent's Data Mode feature allows you to create a data file from a generated document. This feature is useful when you need to extract data from a generated document and use it elsewhere. This can be helpful when you have a large amount of data in your datasources and would like to create a more concise data file for use in other templates or applications.
Creating a data file from a generated document is a simple two-step process:
- Specify the data file to write out to
- Note that the data file can be embedded in office output formats (DOCX, XLSX, PPTX)
- Specify the mode of data to write out
Specify the Data File to Write Out To
To specify the data file you will use the ProcessReport.setDataStream()
method. This method takes in a java.io.OutputStream
object that you will write the data to. The following code snippet demonstrates how to write the data to a file:
report.setDataStream(new FileOutputStream("data_file.xml"));
If you choose to embed the data file in the output document, it is not necessary to specify an output file.
Specify the Mode of Data to Write Out
To specify the data mode you will use the ProcessReport.setDataMode()
method. This method takes in an enum (or multiple enums) that specifies the mode of data to write out. The different modes are:
ProcessReportAPI.DATA_MODE_DATA
: This mode writes out the value returned from the output document in each data node.ProcessReportAPI.DATA_MODE_SELECT
: This mode writes out the select statement used to generate the data in each data node.ProcessReportAPI.DATA_MODE_ALL_ATTRIBUTES
: This mode writes out the tag attributes for each data node.ProcessReportAPI.DATA_MODE_INCLUDE_BITMAPS
: If your output document contains images from tags, this mode will include the images in the data file in uuencoded format.ProcessReportAPI.DATA_MODE_EMBED
: This mode embeds the data file in the output document as data.xml. This is only valid for output formats that support embedded files (DOCX, XLSX, PPTX).
Here are a couple exmples of how to use the setDataMode()
method:
// Sets the data mode to include the data value in each data node
report.setDataMode(ProcessReportAPI.DATA_MODE_DATA);
// This sets the data mode to return both the data value and the select statement in each data node
// as well as embed the data file in the output document
report.setDataMode(ProcessReportAPI.DATA_MODE_EMBED | ProcessReportAPI.DATA_MODE_DATA | ProcessReportAPI.DATA_MODE_SELECT);
As you can see it is very simple to create a data file from a generated document using the Data Mode feature in the Fluent Java Engine.
Below is a full code file that demonstrates how to create a data file from a generated document as well as an attached java project sample that you can use to test this feature:
import java.io.*;
import net.windward.datasource.DataSourceProvider;
import net.windward.datasource.xml.SaxonDataSource;
import net.windward.xmlreport.ProcessDocx;
import net.windward.xmlreport.ProcessReportAPI;
public class DataModeSample {
public static void main(String[] args) {
try {
// To generate a report, first we need a ProcessReport object.
// For now, we're using the DOCX format to output.
FileInputStream template = new FileInputStream("DataModeTemplate.docx");
File outputReport = new File("output\\output.docx");
FileOutputStream reportStream = new FileOutputStream(outputReport);
ProcessDocx report = new ProcessDocx(template, reportStream);
// Create data file stream and link it to the report data stream
FileOutputStream dataFileStream = new FileOutputStream("output\\output_data.xml");
report.setDataStream(dataFileStream);
// Sets the data file to contain the returned data from the tags in the DataModeTemplate
report.setDataMode(ProcessReportAPI.DATA_MODE_DATA);
// Preparation...
report.processSetup();
// Set the datasource and process the data
DataSourceProvider datasource = new SaxonDataSource(new FileInputStream("datasource.xml"));
report.processData(datasource, "SW");
// And... DONE!
report.processComplete();
template.close();
reportStream.close();
System.out.println("Finished generating report: " + fileReport.getAbsolutePath());
System.out.println("Finished generating data file: " + fileReport.getAbsolutePath());
} catch (Exception e) {
// Uh oh, just in case
e.printStackTrace();
}
}
}